#include "BatchCheckOutRequest.h"

BatchCheckOutRequest::BatchCheckOutRequest() {}

BatchCheckOutRequest::BatchCheckOutRequest(const QString &factoryNo, const QString &transId, const QString &eventTime, const QString &eventUser, const QString &machineNo, const QString &batch, const QList<BatchCheckOutPcuInfo> &pcuInfo):
    factoryNo(factoryNo),
    transId(transId),
    eventTime(eventTime),
    eventUser(eventUser),
    machineNo(machineNo),
    batch(batch),
    pcuInfo(pcuInfo)

{

}

QByteArray BatchCheckOutRequest::toJson() const
{
    QJsonObject json;
    int randomNumber = QRandomGenerator::global()->bounded(10000,100000);
    QString dateTime = QDateTime::currentDateTime().toString("yyyyMMddhhmmssSSS");
    json["factoryNo"] = factoryNo;
    json["transId"] = QString("%1%2").arg(dateTime).arg(randomNumber);
    json["eventTime"] = eventTime;
    json["eventUser"] = eventUser;
    json["machineNo"] = machineNo;
    json["batch"] = batch;

    QJsonArray pcuInfoArray;
    for(const auto&item: pcuInfo)
    {
        QJsonObject pcuInfoObj;
        pcuInfoObj["locationNo"]=item.locationNo;
        pcuInfoObj["pcu"]=item.pcu;
        pcuInfoObj["gear"]=item.gear;

        QJsonArray ngCodeArray;
        for(const auto& ngCode:item.ngCodeList)
        {
            ngCodeArray.append(ngCode);
        }
        pcuInfoObj["ngCodeList"] = ngCodeArray;
        pcuInfoArray.append(pcuInfoObj);
    }
    json["pcuInfo"] = pcuInfoArray;

    return QJsonDocument(json).toJson();

}

QString BatchCheckOutRequest::getEndPoint() const
{
    return "/api/getech-mes/IotController/OutboundCheckAndData";
}
